package com.example.securitydemos.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.sql.DataSource;

/**
 * @author linjy on 2023/1/31
 * 会话过期时的处理策略1:跳转到某个指定的URL
 */
//@EnableWebSecurity(debug = true)
public class SecurityConfig_会话过期时的处理策略1 extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**")
                .hasRole("ADMIN")
                .antMatchers("/user/**")
                .hasRole("USER")
                .antMatchers("/app/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf()
                .disable()
                .formLogin()
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                //注销成功，重定向到该路径下
                .logoutSuccessUrl("/login")
                //清除session信息，默认true
                .invalidateHttpSession(true)
                //清除认证信息，默认true
                .clearAuthentication(true)
                .and()
                //进行会话管理
                .sessionManagement()
                //会话固定
                .sessionFixation()
                //设置会话固定防御策略
                .migrateSession()
                //配置会话过期策略
                .invalidSessionUrl("/session/invalid");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

}
